<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    // function func(fn) {
    //   var args = [].slice.call(arguments, 1)

    //   return function () {
    //     var newArgs = args.concat([].slice.call(arguments))
    //     return fn.apply(this, newArgs)
    //   }
    // }

    function curry(fn, length) {
      var len = length || fn.length

      function func(fn) {
        var args = [].slice.call(arguments, 1)

        return function () {
          var newArgs = args.concat([].slice.call(arguments))
          return fn.apply(this, newArgs)
        }
      }

      return function () {
        var newLen = arguments.length

        // 新传入的参数不足最大参数
        if(newLen < len) {
          const formatArg = [fn].concat([].slice.call(arguments))
          return curry(func.apply(this, formatArg))
        } else {
          return fn.apply(this, arguments)
        }
      }
    }

    function add(a, b, c, d) {
      console.log('res: ', a + b + c + d)
    }

    const test = curry(add, 1, 3)
  </script>
</body>

</html>